﻿using Basic.FormRemoteModel;
using Basic.UserFormService.Calculate.Model;
using WeDonekRpc.Helper;

namespace Basic.UserFormService.Calculate.Extend
{
    public static class DictItemExtend
    {
        /// <summary>
        /// 是否为指定的选项文本
        /// </summary>
        /// <param name="value"></param>
        /// <param name="text"></param>
        /// <returns></returns>
        public static bool IsEquals ( RecordVal value, string text )
        {
            if ( value.funcType == DataFuncType.字典项 || value.funcType == DataFuncType.树形字典 )
            {
                if ( value.value == null )
                {
                    return text == null;
                }
                if ( value.dataType == FormRecordDataType.Array )
                {
                    string[] vals = (string[])value.value;
                    return vals.IsExists(a => a.IsNull() == false && value.dicItem.Find(c => c.DicValue == a, c => c.DicText) == text);
                }
                return value.dicItem.Find(a => a.DicValue == value, a => a.DicText) == text;
            }
            else if ( value.funcType == DataFuncType.选项值 )
            {
                if ( value.value == null )
                {
                    return text == null;
                }
                if ( value.dataType == FormRecordDataType.Array )
                {
                    string[] vals = (string[])value.value;
                    return vals.IsExists(a => a.IsNull() == false && value.items.GetValueOrDefault(a) == text);
                }
                return value.items.GetValueOrDefault((string)value) == text;
            }
            return text == value;
        }
        public static bool IsEquals ( RecordVal[] list, string text )
        {
            if ( list.IsNull() )
            {
                return false;
            }
            else if ( !CurState.Index.HasValue )
            {
                return false;
            }
            int index = CurState.Index.Value;
            return IsEquals(list[index], text);
        }
        public static bool IsEquals ( RecordVal left, RecordVal right )
        {
            return left.Equals(right);
        }
    }
}
